// pages/MyCover/sharehetong/sharehetong.js
Page({
  formSubmit: function(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)


    this.clickPup(e.detail.value);
  },
  /**
   * 页面的初始数据
   */
  data: {
    click: false, //是否显示弹窗内容
    option: false, //显示弹窗或关闭弹窗的操作动画
    date: "2020-01-01",
    enddate: "",
    yuedingdate: "",
    jiaimg: "../../../images/home/add.jpg",
    yiimg: "../../../images/home/add.jpg",
    detail: [],
    picUrls: []
  },
  nextclick: function() {
    wx: wx.navigateTo({
      url: '../nextview/nextview',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  bingDateChange: function(e) {
    this.setData({

      date: e.detail.value
    })
  },
  endDateChange: function(e) {
    this.setData({
      enddate: e.detail.value
    })
  },
  yuedingDateChange: function(e) {
    this.setData({
      yuedingdate: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var aa = [];
    // var jsonStr = options.detail.replace(" ", "");
    // if (typeof jsonStr != 'object') {
    //   jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
    //    aa = JSON.parse(jsonStr)

    // }

    aa = wx.getStorageSync("qdetail");

    var array = new Array();
    for (var i = 0; i < aa.length; i++) {
      // var keys = Object.keys(aa)[i];//取到“香蕉1”

      var info = {};
      info["attribute"] = aa[i].attribute;
      // info["value"] = keys;
      info["v"] = aa[i].value;
      array.push(info)

    }


    this.setData({
      detail: array
    })

    wx.reLaunch({

    })
  },
  choosejiaWxImage: function(type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function(res) {
        console.log(res);
        that.setData({
          // tempFilePath可以作为img标签的src属性显示图片
          jiaimg: res.tempFilePaths[0],
        })
      }
    })
  },
  chiosejiaimage: function() {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#aaaaaa",
      success: function(res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.choosejiaWxImage('album')
          } else if (res.tapIndex == 1) {
            that.choosejiaWxImage('camera')
          }
        }
      }
    })
  },

  // 乙的拍照
  chooseyiWxImage: function(type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function(res) {
        console.log(res);
        that.setData({
          // tempFilePath可以作为img标签的src属性显示图片
          yiimg: res.tempFilePaths[0],
        })
      }
    })
  },
  //乙的拍照
  chioseyiimage: function() {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#aaaaaa",
      success: function(res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseyiWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseyiWxImage('camera')
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.showModal({

      title: '提示',

      confirmText: '确定',

      cancelText: '取消',

      content: '页面有数据未保存是否保存',

      success: function(res) {

        if (res.confirm) {

          wx.redirectTo({

            // url: '/pages/index'
          })
        } else if (res.cancel) {
          return;
        }
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  linkclick: function() {

  },


  // 用户点击显示弹窗
  clickPup: function(data) {
    data.bcName = wx.getStorageSync("ssagree");
    data.otherName = wx.getStorageSync("bzOtherTips");
    data.type = 0;

    wx.uploadFile({
      url: 'http://127.0.0.1:8082/wx/storage/upload',
      filePath: this.data.yiimg, //图片路径，如tempFilePaths[0]
      name: 'file',
      header: {
        "Content-Type": "multipart/form-data"
      },
      success: function(res) {
        var _res = JSON.parse(res.data);
        if (_res.errno === 0) {
          data.yipic = _res.data.url
        }


      },
      fail: function(res) {
        console.log(res);
      },
      complete: function(res) {

      }
    })

    wx.uploadFile({
      url: 'http://127.0.0.1:8082/wx/storage/upload',
      filePath: this.data.jiaimg, //图片路径，如tempFilePaths[0]
      name: 'file',
      header: {
        "Content-Type": "multipart/form-data"
      },
      success: function(res) {
        var _res = JSON.parse(res.data);
        if (_res.errno === 0) {
          data.jiapic = _res.data.url
          //提交快捷方式
          wx.request({
            url: "http://127.0.0.1:8082/wx/order/quicksubmit",
            data: data, //{cartId:22},
            method: "POST",
            header: {
              "Content-Type": "application/json",
              'X-Litemall-Token': wx.getStorageSync('token')

            },
            success: function(a) {
              wx.showToast({
                title: '提交成功',
                icon: '',     //默认值是success,就算没有icon这个值，就算有其他值最终也显示success
                duration: 2000,      //停留时间
              })
              wx.navigateBack({
                delta: 1,
              })
            },
            fail: function(a) {
              // t.WxLogin();
            }
          });

        }
      },
      fail: function(res) {
        console.log(res);
      },
      complete: function(res) {

      }
    })


    let _that = this;
    if (!_that.data.click) {
      _that.setData({
        click: true,
      })
    }

    if (_that.data.option) {
      _that.setData({
        option: false,
      })

      // 关闭显示弹窗动画的内容，不设置的话会出现：点击任何地方都会出现弹窗，就不是指定位置点击出现弹窗了
      setTimeout(() => {
        _that.setData({
          click: false,
        })
      }, 500)


    } else {
      _that.setData({
        option: true
      })
    }
  },

})